/* eslint-disable */
// import parseTime, formatTime and set to filter
export { parseTime, formatTime } from '@/utils';

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label;
  }
  return `${time + label}s`;
}

export function hidePhone(phone) {
  return phone.toString().replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2');
}
/**
 * 时间格式化
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time);
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute');
  } if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour');
  }
  return pluralize(~~(between / 86400), ' day');
}

/**
 * 存储单位换算
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [{
    value: 1E18,
    symbol: 'E',
  },
  {
    value: 1E15,
    symbol: 'P',
  },
  {
    value: 1E12,
    symbol: 'T',
  },
  {
    value: 1E9,
    symbol: 'G',
  },
  {
    value: 1E6,
    symbol: 'M',
  },
  {
    value: 1E3,
    symbol: 'k',
  },
  ];
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol;
    }
  }
  return num.toString();
}

/**
 * 数字单位整理
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
}
/**
 * 数字万位换算
 * @param {number} num
 */
export function toTransfromTenThousand(num) {
  return parseInt(Number(num) / 100) / 100 + '万'
}
/**
 * 首字母大写
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
 * 资源地址处理过滤器
 * @param {string} str 资源地址
 */
export function resourceUrl(str) {
  if (!str) {
    return str;
  }
  if (str.indexOf('//') !== -1) {
    return str;
  }
  return `https://resource.miva.work/${str}`;
}
